package com.ylxn.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.ylxn.utils.RabbitMqUtils;
import com.ylxn.utils.SleepUtils;

/**
 * @author cenghaile
 * @version 1.0
 * 2021/7/1 2:08 下午
 **/
public class Work03 {
    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("work03等待消费消息_时间长");
        DeliverCallback deliverCallback = (consumerTag, message)->{
            //沉睡一秒中
            SleepUtils.sleep(30);
            System.out.println("接受到的消息"+new String(message.getBody(), "UTF-8"));
            //手动应答
            /**
             * 参数一：消息的标记
             * 参数二：是否批量应答信道中的消息
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        //采用手动应答
        boolean ack_auto = false;
        channel.basicConsume(QUEUE_NAME,ack_auto,deliverCallback,(cousumer ->{
            System.out.println("消费者取消消费");
        }));
    }
}
